﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace BaseExtendsions;

/// <summary>
/// json设置
/// </summary>
public static class JsonSetting
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="jsonSerializerSettings"></param>
    public static JsonSerializerSettings SetSetting(this  JsonSerializerSettings jsonSerializerSettings)
    {
        jsonSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
        // jsonSerializerSettings.TypeNameHandling = TypeNameHandling.All;
        // 忽略循环引用
        jsonSerializerSettings.Converters.Add(new LongToStringConverter());
        jsonSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        // 使用小驼峰
        jsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); // new DefaultContractResolver();
        //jsonSerializerSettings.MaxDepth = 3;
        jsonSerializerSettings.Formatting = Formatting.Indented;
        //忽略空值 不包含属性的null序列化
        jsonSerializerSettings.NullValueHandling = NullValueHandling.Include;
        //忽略默认值和null 1、不包含属性默认值和null
        jsonSerializerSettings.DefaultValueHandling = DefaultValueHandling.Include;
        return jsonSerializerSettings;

        // 对 JSON 数据使用混合大小写。跟属性名同样的大小.输出
        //  jsonSerializerSettings.ContractResolver = new DefaultContractResolver();
        //  jsonSerializerSettings.Converters.Add(new NewtonsoftLongConverter());
    }
}
